﻿using MyabpDDD.Demo.Application.FuelmanagementtableLY.Dto;
using MyCompany.MyProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyabpDDD.Demo.Application.FuelmanagementtableLY
{
    public class FuelmanagementtableLyService : ApplicationService, IFuelmanagementtableLyService
    {
        private readonly IRepository<Fuelmanagementtable> _repository;
        private readonly IRepository<Agenttable> _agenttables;
        public FuelmanagementtableLyService(IRepository<Fuelmanagementtable> students, IRepository<Agenttable> classes)
        {
            this._repository = students;
            this._agenttables = classes;
        }

        public async Task Delete(int id)
        {
            var list= _repository.Where(r => r.Id == id).FirstOrDefault();

             await _repository.DeleteAsync(list);
        }

        public dynamic Get(int id)
        {
            var list=_repository.Where(r => r.Id == id).ToList();
            var q2 = _agenttables.AsQueryable();
            var query = from a in list
                        join a2 in q2 on a.Agent equals a2.Id
                        select new FuelmanagementtableLyDto()
                        {
                            Id = a.Id,
                            Licenseplate = a.Licenseplate,
                            Comeonost = a.Comeonost,
                            Amount = a.Amount,
                            Mileage = a.Mileage,
                            Payment = a.Payment,
                            Agent = a.Agent,
                            Note = a.Note,
                            Creationtime = a.Creationtime,
                            AgenttaName = a2.AgenttaName
                        };
            var yuhe = query.ToList();
            var yuhd = yuhe[0];
            return yuhd;
        }

        //public async Task<bool> Delete(int id)
        //{
        //    var model=await _repository.GetAsync(d=>d.Id==id);
        //    if (model != null)
        //    {
        //        _repository.DeleteAsync(model);
        //    }
        //    return true;

        //}

        public async Task<Fuelmanagementtable> Inser(FuelmanagementtableLyDto dto)
        {
            //Fuelmanagementtable yuh = new Fuelmanagementtable();
            //var s=ObjectMapper.Map<FuelmanagementtableLyDto, Fuelmanagementtable>(dto);

            //yuh.Licenseplate=dto.Licenseplate;
            //yuh.Comeonost=dto.Comeonost;
            //yuh.Amount=dto.Amount;
            //yuh.Mileage=dto.Mileage;成了 
            //yuh.Payment=dto.Payment;
            //yuh.Agent=dto.Agent;这次是代码有错了
            //yuh.Note=dto.Note;上面的报错没影响吗
            //yuh.Creationtime=dto.Creationtime;z
            //return await  _repository.InsertAsync(s);
            //var q2 = _agenttables.AsQueryable();
            //var yu=q2.Where(x=>x.AgenttaName==dto.AgenttaName).ToList();
            ////if (yu.Count() == 0)
            ////{
            //Agenttable a = new Agenttable();
            //a.AgenttaName = dto.AgenttaName;
            ////Agenttable yu78 = ObjectMapper.Map<AgenttableDto, Agenttable>(a);
            ////新增是iD使用Guid，有C#代码来创建GUID
            //await _agenttables.InsertAsync(a);

            //var q22 = _agenttables.AsQueryable();
            //var yu2 = q22.Where(x => x.AgenttaName == dto.AgenttaName).ToList();
            //dto.Agent = yu2[0].Id;
            //}
            dto.Creationtime = DateTime.Now;
            Fuelmanagementtable www=ObjectMapper.Map<FuelmanagementtableLyDto, Fuelmanagementtable>(dto);
            return await _repository.InsertAsync(www);
        }

      

        public dynamic QueryStudentAll(int pageIndex, int pageSize, string chepai, int? jid)
        {
            var q1 = _repository.AsQueryable();
            var q2 = _agenttables.AsQueryable();
            var query = from a in q1
                        join a2 in q2 on a.Agent equals a2.Id
                        select new FuelmanagementtableLyDto()
                        {
                            Id = a.Id,
                            Licenseplate=a.Licenseplate,
                            Comeonost=a.Comeonost,
                            Amount=a.Amount,
                            Mileage=a.Mileage,
                            Payment=a.Payment,
                            Agent=a.Agent,
                            Note=a.Note,
                            Creationtime=a.Creationtime,
                           AgenttaName=a2.AgenttaName
                        };
            if (!string.IsNullOrEmpty(chepai))
            {
                query=query.Where(x=>x.Licenseplate==chepai);
            }
            //if (jid > 0 && jid != null)
            //{
            //    query=query.Where(x=>x.Agent==jid);
            //}
            var list = new
            {
                Zongtiao = query.Count(),
                data = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()
            };
            return list;
        }

        public bool SelectDel(string ids)
        {
            string[] int_ids = ids.Split(',');
            var q = from id in int_ids
                    select Convert.ToInt32(id);
            var q_list = q.ToList();
            var query = _repository.AsQueryable();
            query = query.Where(d => q_list.Contains
              (d.Id));
            foreach (var model in query)
            {
                 _repository.DeleteAsync(model);
            }
            return true;

        }

        public async Task<bool> update(FuelmanagementtableLyDto dto)
        {
            //Fuelmanagementtable s = ObjectMapper.Map<FuelmanagementtableLyDto, Fuelmanagementtable>(dto);
            Fuelmanagementtable yuh = new Fuelmanagementtable();
            yuh.Licenseplate = dto.Licenseplate;
            yuh.Comeonost = dto.Comeonost;
            yuh.Amount = dto.Amount;
            yuh.Mileage = dto.Mileage;
            yuh.Payment = dto.Payment;
            yuh.Agent = dto.Agent;
            yuh.Note = dto.Note;
            yuh.Creationtime = dto.Creationtime;
            var db = _repository.GetDbSet<Fuelmanagementtable>();
            db.Update(yuh);
            return true;
        }
    }
}
